import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:osc/models/entity/osc_post_entity.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/utils/pd_util.dart';
import 'package:osc/utils/time_util.dart';

import '../real_rich_text.dart';

class RichTextTitle extends StatelessWidget {
  final OscPostEntity item;

  const RichTextTitle({Key key, @required this.item}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    List<TextSpan> textSpans = List<TextSpan>();
    List<String> tags = item.tags;
    if(TimeUtil.isToday(item.pubDate)){
      textSpans.add(buildImageSpan("ic_label_today", EdgeInsets.only(right: Dimens.gap_view_small)));
    }
    if(tags != null && tags.length > 0){
      for(String tag in tags) {
        textSpans.add(buildImageSpan("ic_label_$tag", EdgeInsets.only(right: Dimens.gap_view_smaller)));
      }
    }
    textSpans.add(buildTextSpan(item.title));
    return RealRichText(
      textSpanList:textSpans,
    );
  }

  buildTextSpan(String text){
    return TextSpan(
      text: text,
      style: TextStyle(color: Colors.black, fontSize: Dimens.font_sp14, fontWeight: FontWeight.bold),
    );
  }

  buildImageSpan(String imageName, EdgeInsets margin){
    return ImageSpan(
      AssetImage(PdUtil.getImagePath(imageName)),
      imageWidth: Dimens.view_item_tag_height_small,
      imageHeight: Dimens.view_item_tag_height_small,
      margin: margin,
    );
  }

}
